#!/usr/bin/make -f

include /usr/share/quilt/quilt.make

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

# Get the SVN revision from the changelog
SVNVERSION ?= $(shell dpkg-parsechangelog | grep '^Version:' | sed 's/.*svn\([[:digit:]]*\).*/\1/')

# List of options to pass to configure. Can be overridden.
XBMC_CONFIG_OPTIONS ?= --host=$(DEB_HOST_GNU_TYPE) \
	--build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr --disable-ccache \
	--disable-dvdcss --enable-external-libraries --enable-avahi

# Add the SVN revision if it exists in the changelog
ifeq (,$(findstring Version,$(SVNVERSION)))
  XBMC_CONFIG_OPTIONS += SVN_REV="$(SVNVERSION)"
endif

%:
	dh $@

override_dh_clean:
	dh_clean
	rm -f debian/xbmc-live.init
	debian/rules unpatch

override_dh_auto_configure: patch
	cp -f tools/XBMCLive/xbmc debian/xbmc-live.init
	./configure $(XBMC_CONFIG_OPTIONS)

override_dh_auto_install:
	$(MAKE) install install-livedatas prefix=$(CURDIR)/debian/tmp/usr
	$(MAKE) eventclients prefix=$(CURDIR)/debian/tmp/usr \
		installdir=/usr WII_EXTRA_OPTS=-DCWIID_OLD
	mkdir -p $(CURDIR)/debian/tmp/usr/share/applications \
		$(CURDIR)/debian/tmp/usr/share/pixmaps
	cp $(CURDIR)/tools/Linux/xbmc.png \
		$(CURDIR)/debian/tmp/usr/share/pixmaps/
	cp $(CURDIR)/tools/Linux/xbmc.desktop \
		$(CURDIR)/debian/tmp/usr/share/applications/

override_dh_install:
	dh_install --sourcedir=$(CURDIR)/debian/tmp -XLICENCE \
		-XLiberationSans-Regular.ttf -XLicence.txt -XLicense.txt
	# XBMC WEB EFLEXWEB
	mkdir -p $(CURDIR)/debian/xbmc-web-eflexweb/usr/share/xbmc/web
	unzip -oq $(CURDIR)/web/WebInterfaceXBMC-0.17.zip \
		-d $(CURDIR)/debian/xbmc-web-eflexweb/usr/share/xbmc/web
	# XBMC WEB PM
	mkdir -p $(CURDIR)/debian/xbmc-web-pm/usr/share/xbmc/web
	unzip -oq $(CURDIR)/web/Project_Mayhem_webserver_V1.1.2.zip \
		-d $(CURDIR)/debian/xbmc-web-pm/usr/share/xbmc/web \
		-x "*/Thumbs.db"
	# XBMC WEB Iphone
	mkdir -p $(CURDIR)/debian/xbmc-web-iphone-tlrobinson/usr/share/xbmc/web/iphone
	unzip -oq $(CURDIR)/web/xbmciphone-0.1.zip \
		-d $(CURDIR)/debian/xbmc-web-iphone-tlrobinson/usr/share/xbmc/web
	mkdir -p $(CURDIR)/debian/xbmc-web-iphone-tlrobinson/usr/share/doc/xbmc-web-iphone-tlrobinson/ 
	mv $(CURDIR)/debian/xbmc-web-iphone-tlrobinson/usr/share/xbmc/web/README \
	    $(CURDIR)/debian/xbmc-web-iphone-tlrobinson/usr/share/doc/xbmc-web-iphone-tlrobinson/

override_dh_link:
	dh_link
	install -D "debian/xbmc-common/usr/share/xbmc/xbmc.bin" \
		"debian/xbmc-common/usr/lib/xbmc/xbmc.bin"
	dh_link -pxbmc-common "usr/lib/xbmc/xbmc.bin" \
		"usr/share/xbmc/xbmc.bin"
	install -D "debian/xbmc-common/usr/share/xbmc/xbmc-xrandr" \
		"debian/xbmc-common/usr/lib/xbmc/xbmc-xrandr"
	dh_link -pxbmc-common "usr/lib/xbmc/xbmc-xrandr" \
		"usr/share/xbmc/xbmc-xrandr"
	find "debian/xbmc-common/usr/share/xbmc" -regextype posix-extended \
		-type f -iregex ".*\.so|.*\.vis|.*\.xbs" | while read FILE; do \
		NEW_LOCATION="$$(echo $$FILE | sed -e 's|/usr/share|/usr/lib|')"; \
		LINK_TARGET="$$(echo "$$NEW_LOCATION" | sed -e 's|debian/xbmc-common/||')"; \
		LINK_DESTINATION="$$(echo "$$FILE" | sed -e 's|debian/xbmc-common/||')"; \
		install -D "$$FILE" "$$NEW_LOCATION"; \
		dh_link -pxbmc-common "$$LINK_TARGET" "$$LINK_DESTINATION"; \
		done

override_dh_strip:
	dh_strip --dbg-package=xbmc-dbg

override_dh_makeshlibs:
	# We don't install shared libraries in standard locations so don't edit
	# postinst/postrm scripts to call ldconfig
	dh_makeshlibs -n
